package com.shawn.spring.boot.mybatisplus.run;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shawn.spring.boot.mybatisplus.constants.MenuTypeEnum;
import com.shawn.spring.boot.mybatisplus.entity.Menu;
import com.shawn.spring.boot.mybatisplus.entity.Role;
import com.shawn.spring.boot.mybatisplus.entity.RoleMenu;
import com.shawn.spring.boot.mybatisplus.entity.User;
import com.shawn.spring.boot.mybatisplus.entity.User.UserBuilder;
import com.shawn.spring.boot.mybatisplus.entity.UserRole;
import com.shawn.spring.boot.mybatisplus.service.MenuService;
import com.shawn.spring.boot.mybatisplus.service.RoleMenuService;
import com.shawn.spring.boot.mybatisplus.service.RoleService;
import com.shawn.spring.boot.mybatisplus.service.UserRoleService;
import com.shawn.spring.boot.mybatisplus.service.UserService;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

/**
 * @author shawn
 * @since 2020/8/30
 */
@Component
@Slf4j
public class MybatisPlusRunner implements ApplicationRunner {

  @Autowired private UserService userService;
  @Autowired private RoleService roleService;
  @Autowired private MenuService menuService;
  @Autowired private RoleMenuService roleMenuService;
  @Autowired private UserRoleService userRoleService;

  @Override
  public void run(ApplicationArguments args) throws Exception {
    newMenu();
    queryMenuById();
    newRole();
    queryRoleById();
    queryByLikeName();
    queryMenuByRoleId();
    newUser();
    login();
    queryRolesByUserId();
    queryMenusByPid();
    testPageQuery();
  }

  public void newMenu() {
    Menu menu =
        Menu.builder()
            .name("系统管理")
            .orderNum(1)
            .parentId(Long.valueOf(0))
            .url("/")
            .type(MenuTypeEnum.CATALOG_TYPE.getCode())
            .isDeleted(0)
            .build();
    menuService.save(menu);
    log.info("success create new menu");
  }

  public void queryMenuById() {
    Menu menu = menuService.getById(1);
    log.info("queryMenuById: {}", menu.toString());
  }

  public void newRole() {
    Role role = Role.builder().remark("系统管理员").roleName("系统管理员").isDeleted(0).build();
    int key = roleService.saveReturnKey(role);
    roleMenuService.save(RoleMenu.builder().menuId(1l).roleId(Long.valueOf(key)).build());
    log.info("success create new role");
  }

  public void queryRoleById() {
    log.info("queryRoleById: {}", roleService.getById(2));
  }

  public void queryByLikeName() {
    QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
    queryWrapper.like("role_name", "系统");
    roleService.list(queryWrapper);
    List<Role> roleList = roleService.list(queryWrapper);
    if (!CollectionUtils.isEmpty(roleList)) {
      roleList.forEach(role -> log.info("queryByLikeName: {}", role.toString()));
    }
  }

  public void queryMenuByRoleId() {
    QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("role_id", 2);
    List<RoleMenu> menuList = roleMenuService.list(queryWrapper);
    if (!CollectionUtils.isEmpty(menuList)) {
      menuList.forEach(menu -> log.info("queryMenuByRoleId: {}", menu.toString()));
    }
  }

  public void newUser() {
    User shawn =
        User.builder()
            .email("1111@qq.com")
            .mobile("12345678901")
            .password("123456")
            .salt("12345")
            .username("shawn")
            .isDeleted(0)
            .build();
    int i = userService.saveReturnKey(shawn);
    userRoleService.save(UserRole.builder().roleId(1l).userId(Long.valueOf(i)).build());
    log.info("success create new user");
  }

  public void login() {
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("username", "admin").and(wrapper -> wrapper.eq("password", "123456"));
    List<User> list = userService.list(queryWrapper);
    if (CollectionUtils.isEmpty(list) || list.size() > 1) {
      log.error("login failed! username: {}, password: {}", "shawn", "123456");
    } else {
      User shawn = list.get(0);
      log.info("login success! {}", shawn.toString());
    }
  }

  public void queryRolesByUserId() {
    QueryWrapper<UserRole> wrapper = new QueryWrapper<>();
    wrapper.eq("user_id", 1l);
    List<UserRole> roleList = userRoleService.list(wrapper);
    if (!CollectionUtils.isEmpty(roleList)) {
      roleList.forEach(role -> log.info("queryRolesByUserId: {}", role.toString()));
    }
  }

  public void queryMenusByPid() {
    QueryWrapper<Menu> wrapper = new QueryWrapper<>();
    wrapper.eq("parent_id", 0);
    List<Menu> menuList = menuService.list(wrapper);
    if (!CollectionUtils.isEmpty(menuList)) {
      menuList.forEach(menu -> log.info("queryMenusByPid: {}", menu.toString()));
    }
  }

  public void testPageQuery() {
    Page<User> page = new Page<>(1, 3);
    userService.selectByPage(page);
    log.info("data: {}", page.getRecords());
    log.info("total: {}", page.getTotal());
  }
}
